package dk.dashboardFP.interactionElementFramwork {
	import dk.dashboardFP.interactionElementFramwork.focusHandler.IFocusableItem;

	import flash.display.InteractiveObject;
	import flash.events.EventDispatcher;

	/**
	 * @author jk
	 */
	public class InteractionElement extends EventDispatcher  implements IFocusableItem, IInteractivElementHock {

		protected var _doIEV : InteractionElementView;
		private var _bSelected : Boolean = false;
		private var _bHidden : Boolean;
		private var _bInFocus : Boolean;
		private var _bDisabled : Boolean = false;
		protected var _bOwnsKeyFocus : Boolean = false;
		private var _bDisableOnSelection : Boolean = true;		private var _bCancelMouseOverOutIfSelected : Boolean = false;		public function InteractionElement(oIEV:InteractionElementView){
			_doIEV = oIEV;
			_doIEV.registerIEHock(this);
			super();
			setup();
		}
		
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// setup
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		private function setup() : void {
			registerBasicViewEvents();
		}
		
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// setFocused
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		public function setInFocus() : void {
			if (!_bDisabled) {
				onFocusIn();
			}
		}
		
		public function setOutFocus() : void {
			onFocusOut();
		}
		
		protected function onFocusOut() : void {
			if (_bInFocus) {
				_bInFocus=false;				
				_doIEV.focusOut();
				dispatchEvent(new InteractionElementEvent(InteractionElementEvent.FOCUS_OUT, null));
			}
		}

		protected function onFocusIn() : void {
			
			if (!_bInFocus) {
				_bInFocus=true;
				_doIEV.focusIn();
				dispatchEvent(new InteractionElementEvent(InteractionElementEvent.FOCUS_IN, null));
			}
		}

		public function getDisplayObjectForFocusListening() : InteractiveObject {
			return _doIEV;
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// basic view event
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		protected function registerBasicViewEvents() : void {
			_doIEV.addEventListener(InteractionElementViewEvent.MOUSE_OVER_VIEW, onMouseOverView, false, 0, true);
			_doIEV.addEventListener(InteractionElementViewEvent.MOUSE_OUT_VIEW, onMouseOutView, false, 0, true);
			
			_doIEV.addEventListener(InteractionElementViewEvent.MOUSE_UP_VIEW, onMouseUpView, false, 0, true);
			_doIEV.addEventListener(InteractionElementViewEvent.MOUSE_DOWN_VIEW, onMouseDownView, false, 0, true);
		}
		
		protected function onMouseDownView(event : InteractionElementViewEvent) : void {
			_doIEV.mouseDown();
		}

		protected function onMouseUpView(event : InteractionElementViewEvent) : void {
			_doIEV.mouseUp();
		}

		protected function onMouseOutView(event : InteractionElementViewEvent) : void {
			if (bDisabled) {
				_doIEV.mouseOutDisabled();
			} else if (bSelected) {
				_doIEV.mouseOutSelected();
			} else {
				_doIEV.mouseOut();
			}
		}

		protected function onMouseOverView(event : InteractionElementViewEvent) : void {
			if (bDisabled) {
				_doIEV.mouseOverDisabled();			
			} else if (bSelected) {
				_doIEV.mouseOverSelected();
			} else {
				_doIEV.mouseOver();
			}
		}
		
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// selected handler
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		public function setSelected() : void {
			if (!_bSelected) {
				doSetSelected();
			}
		}
		
		public function setDeSelected() : void {
			if (_bSelected) {
				doSetDeSelected();
			}
		}
		
		private function doSetDeSelected() : void {
			_bSelected=false;
			_doIEV.deSelect();
		}		

		private function doSetSelected() : void {
			_bSelected=true;
			_doIEV.select();
		}
		
		public function toggleSelection() : void {
			if (_bSelected) {
				doSetDeSelected();
			} else {
				doSetSelected();
			}
		}
		

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// tween in out
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		public function tweenIn(nDelay : Number=0) : void {
			_doIEV.tweenIn(nDelay);
		}

		public function tweenOut(nDelay : Number=0):void {
			_doIEV.tweenOut(nDelay);
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// disabled
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		public function disable() : void {
			if (!_bDisabled) {
				if(_bInFocus) {
					setOutFocus();
				}
				_bDisabled=true;
				_doIEV.disable();
			}
		}
		
		public function enable() : void {
			if (_bDisabled) {
				_bDisabled=false;
				_doIEV.enable();
			}
		}
		
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// hide unhide
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		public function hide() : void {
			if (!_bHidden) {
				doHide();
			}
		}
		
		private function doHide() : void {
			_bHidden=true;
			_doIEV.hide();
		}

		public function show() : void {
			if (_bHidden) {
				doShow();
			}
		}
		
		private function doShow() : void {
			_bHidden=false;
			_doIEV.show();
		}
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// implementation of view props
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		public function get x() : Number { 
			return _doIEV.x; 
		}
		
		public function set x(oNewValue:Number) : void { 
			_doIEV.x = oNewValue;
		}
		
		public function get y() : Number { 
			return _doIEV.y; 
		}
		
		public function set y(oNewValue:Number) : void { 
			_doIEV.y = oNewValue;
		}
		
		public function get visible() : Boolean { 
			return _doIEV.visible; 
		}
		
		public function set visible(oNewValue:Boolean) : void { 
			_doIEV.visible =oNewValue;
		}
		
		public function get scaleX() : Number { 
			return _doIEV.scaleX; 
		}
		
		public function set scaleX(oNewValue:Number) : void { 
			_doIEV.scaleX = oNewValue;
		}
		
		public function get scaleY() : Number { 
			return _doIEV.scaleY; 
		}
		
		public function set scaleY(oNewValue:Number) : void { 
			_doIEV.scaleY = oNewValue;
		}
		
		public function get height() : Number { 
			return _doIEV.height; 
		}
		
		public function set height(oNewValue : Number) : void { 
			_doIEV.height = oNewValue; 
		}
		
		public function get width() : Number { 
			return _doIEV.width; 
		}
		
		public function set width(oNewValue:Number) : void { 
			_doIEV.width = oNewValue;
		}
		
		
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// cleanup
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// getter setters
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		public function get doView() : InteractionElementView {
			return _doIEV;
		}
		
		public function get bHidden() : Boolean {
			return _bHidden;
		}
		
		public function get bSelected() : Boolean {
			return _bSelected;
		}

		public function get bInFocus() : Boolean {
			return _bInFocus;
		}
		
		public function get bDisableOnSelection() : Boolean { 
			return _bDisableOnSelection; 
		}
		
		public function get bDisabled() : Boolean {
			return _bDisabled;
		}				public function get bOwnsKeyFocus() : Boolean {
			return _bOwnsKeyFocus;
		}
		
		public function set bDisableOnSelection(bDisableOnSelection : Boolean) : void {
			_bDisableOnSelection = bDisableOnSelection;
		}
		
		public function get bCancelMouseOverOutIfSelected() : Boolean { 
			return _bCancelMouseOverOutIfSelected; 
		}
		
		public function set bCancelMouseOverOutIfSelected(oNewValue:Boolean) : void { 
			_bCancelMouseOverOutIfSelected=oNewValue;
		}
	}
}
